// formPackage/pages/invite/inviteList/inviteList.js

import { queryMyInviteList, cancleOneInvite, pushOrDownInvite } from "../../../../service/invite_service"

let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    inviteList: [

    ],
    exStatus: 0,
    examineStatus: 1,
    switchArr: [

      {
        title: "待审核",
        key: 1,
      },
      {
        title: "审核成功",
        key: 2
      },
      {
        title: "审核失败",
        key: 3
      },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
    this.queryMyInviteList();
  },
  switchAction(e) {
    let index = e.currentTarget.dataset.index;
    let item = e.currentTarget.dataset.item;
    this.setData({
      exStatus: index,
      examineStatus: item.key
    })
    this.queryMyInviteList();
  },
  //获取我自己的招募列表:
  queryMyInviteList() {
    wx.showLoading();
    queryMyInviteList(this.data.examineStatus).then(res => {
      wx.hideLoading();
      console.log("JJLog:获取到的招募列表是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          inviteList: res.data
        })
      }
    }).catch(err => {
      console.error("JJLog:queryMyInviteList", err);
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
    })
  },

  //删除一条招募
  rightCancelBtnAction(e) {
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: '是否删除当前招募信息',
      success: res => {
        if (res.confirm) {
          this.cancleOneInvite(id, index);
        }
      }
    })

  },
  //删除请求:
  cancleOneInvite(id, index) {
    wx.showLoading();
    cancleOneInvite(id).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: '删除成功',
          icon: "success"
        })
        let arr = this.data.inviteList;
        arr.splice(index, 1);
        this.setData({
          inviteList: arr
        })
      }
    }).catch(err => {
      wx.hideLoading();
      console.error("JJLog:cancleOneInvite", err);
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
    })
  },

  //上架或者下架请求:
  pushUpOrDown(e) {
    let item = e.currentTarget.dataset.item;
    let index = e.currentTarget.dataset.index;
    this.pushOrDownInvite(item.id, item.isPush, index);
  },
  //上下架请求
  pushOrDownInvite(id, isPush, index) {
    wx.showLoading();
    pushOrDownInvite(id).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        let item = 'inviteList[' + index + '].isPush';
        this.setData({
          [item]: !isPush,
        })
      }
    }).catch(err => {
      console.error("JJLog:pushOrDownInvite", err);
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
    })
  },

  //跳转到编辑页面:
  pushToEdit(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/formPackage/pages/invite/publishInvite/publishInvite?isPublish=0' + "&id=" + id,
    })
  },
  //跳转到发布页面:
  pushToPublish() {
    if (!app.globalData.companyNumber) {
      //没有公司号的就提示去入驻公司才能发布:
      wx.showToast({
        title: "入驻公司或者公司成员才能发布招聘",
        icon: "none"
      })
      return;
    }
    wx.navigateTo({
      url: '/formPackage/pages/invite/publishInvite/publishInvite?isPublish=1',
    })
  },

})